/*******************************************************************************
** Qt Advanced Docking System
** Copyright (C) 2017 Uwe Kindler
** 
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
** 
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** Lesser General Public License for more details.
** 
** You should have received a copy of the GNU Lesser General Public
** License along with this library; If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/


//============================================================================
/// \file   DockSplitter.cpp
/// \author Uwe Kindler
/// \date   24.03.2017
/// \brief  Implementation of CDockSplitter
//============================================================================

//============================================================================
//                                   INCLUDES
//============================================================================
#include "DockSplitter.h"

#include <QDebug>
#include <QChildEvent>
#include <QVariant>
#include "DockAreaWidget.h"

namespace ads
{
// 私有的分隔栏数据结构
struct DockSplitterPrivate
{
    CDockSplitter* _this; // 分隔栏指针
    int VisibleContentCount = 0; // 可见内容数量
    DockSplitterPrivate(CDockSplitter* _public) : _this(_public) {}
};

// 构造函数，设置属性和子控件不可折叠
CDockSplitter::CDockSplitter(QWidget *parent)
    : QSplitter(parent),
      d(new DockSplitterPrivate(this))
{
    setProperty("ads-splitter", QVariant(true));
    setChildrenCollapsible(false);
}

// 构造函数
CDockSplitter::CDockSplitter(Qt::Orientation orientation, QWidget *parent)
    : QSplitter(orientation, parent),
      d(new DockSplitterPrivate(this))
{
}

// 析构函数，打印信息并释放私有数据
CDockSplitter::~CDockSplitter()
{
    ADS_PRINT("~CDockSplitter");
    delete d;
}

// 判断是否有可见的内容
bool CDockSplitter::hasVisibleContent() const
{
    // TODO 缓存或预计算以加速
    for (int i = 0; i < count(); ++i)
    {
        if (!widget(i)->isHidden())
        {
            return true;
        }
    }
    return false;
}

// 获取第一个子控件
QWidget* CDockSplitter::firstWidget() const
{
    return (count() > 0) ? widget(0) : nullptr;
}

// 获取最后一个子控件
QWidget* CDockSplitter::lastWidget() const
{
    return (count() > 0) ? widget(count() - 1) : nullptr;
}

// 判断是否随容器一起调整大小
bool CDockSplitter::isResizingWithContainer() const
{
    // 查找所有子控件中是否有中央控件区域
    for (auto area : findChildren<CDockAreaWidget*>())
    {
        if(area->isCentralWidgetArea())
        {
            return true;
        }
    }
    return false;
}

} // namespace ads

//---------------------------------------------------------------------------
// EOF DockSplitter.cpp
